<?php
/*
* Webing - The SNS blog
*
* Copyright (c) 2009 Webing. All rights reserved.
*
* This is file class
*/
/*$Id: file.php 60 2009-08-11 08:55:10Z chopins.xiao $;*/
class file
{
    public function __construct()
    {
        
    }
    public function chkdir($dir)
    {
        if($this->chkperms($dir) && is_dir($dir))
        {
            return TRUE;
        }else{
            return FALSE;
        }
    }
    private function chkperms($dir)
    {
        if(is_writable($dir))
        {
            return 'w';
        }elseif(is_readable($dir))
        {
            return 'r';
        }else{
            return FALSE;
        }
    }
    public function read_file($file, $type = 'ALL')
    {
        if(!$this->chkperms($file))
        {
            return FALSE;
        }
        $type = strtoupper($type);
        if($type == 'ALL')
        {
            return file_get_contents($file);
        }
        $fp = fopen($file, 'rb');
        if($type == 'LINE')
        {
            return fgets($fp);
        }
        if(is_numeric($type))
        {
            if($type > 8192)
            {
                while (!feof($fp)) {
                    $contents .= fread($fp, 8192);
                    $len = strlen($contents);
                    $last = $type - $len;
                    if($last < 8192)
                    {
                        $contents .= fread($fp, $last);
                        break;
                    }
                }
            }else{
                $contents = fread($fp, $type);
            }
        }else{
            fclose($fp);
            return FALSE;
        }
        fclose($fp);
        return $contents;
    }

    public function write_file($path, $data, $mode = 'r+')
    {
        if ( ! $fp = @fopen($path, $mode))
        {
            return FALSE;
        }

        flock($fp, LOCK_EX);
        fwrite($fp, $data);
        flock($fp, LOCK_UN);
        fclose($fp);

        return TRUE;
    }

    public function delete_files($path, $del_dir = FALSE, $level = 0)
    {
        // Trim the trailing slash
        $path = preg_replace("|^(.+?)/*$|", "\\1", $path);

        if ( ! $current_dir = @opendir($path))
        return;

        while(FALSE !== ($filename = @readdir($current_dir)))
        {
            if ($filename != "." and $filename != "..")
            {
                if (is_dir($path.'/'.$filename))
                {
                    // Ignore empty folders
                    if (substr($filename, 0, 1) != '.')
                    {
                        delete_files($path.'/'.$filename, $del_dir, $level + 1);
                    }
                }
                else
                {
                    unlink($path.'/'.$filename);
                }
            }
        }
        @closedir($current_dir);

        if ($del_dir == TRUE AND $level > 0)
        {
            @rmdir($path);
        }
    }

    public function get_file_info($file, $returned_values = array('name', 'server_path', 'size', 'date'))
    {

        if ( ! file_exists($file))
        {
            return FALSE;
        }

        if (is_string($returned_values))
        {
            $returned_values = explode(',', $returned_values);
        }

        foreach ($returned_values as $key)
        {
            switch ($key)
            {
                case 'name':
                    $fileinfo['name'] = substr(strrchr($file, '/'), 1);
                    break;
                case 'server_path':
                    $fileinfo['server_path'] = $file;
                    break;
                case 'size':
                    $fileinfo['size'] = filesize($file);
                    break;
                case 'date':
                    $fileinfo['date'] = filectime($file);
                    break;
                case 'readable':
                    $fileinfo['readable'] = is_readable($file);
                    break;
                case 'writable':
                    // There are known problems using is_weritable on IIS.  It may not be reliable - consider fileperms()
                    $fileinfo['writable'] = is_writable($file);
                    break;
                case 'executable':
                    $fileinfo['executable'] = is_executable($file);
                    break;
                case 'fileperms':
                    $fileinfo['fileperms'] = fileperms($file);
                    break;
            }
        }

        return $fileinfo;
    }
}
?>